/******************************************************************************
*   Filename:   SchM.c 
*
*   Description:
*
*
*   Revision History:
*
*   Date          #Change       Author     
*   MM-DD-YY      Number:       Initials   Description of change
*   -----------   -----------   --------   ------------------------------------
*   03-01-14       02            SPA        OS Task Initial Release
******************************************************************************/

/******************************************************************************
*   Include Files
******************************************************************************/ 
#include <mc9s12xep100.h>
#include "SchM.h"
#include "SchM_Tasks.h"
#include "SchM_Cfg.h"
/******************************************************************************
*   Local Macro Definitions
******************************************************************************/ 
#define SCHM_OSTICK_ENABLED ((u8)1)
#define SCHEDULER_ENABLED ((u8)1)
/******************************************************************************
*   Local Type Definitions
******************************************************************************/

/******************************************************************************
*   Local Function Declarations
******************************************************************************/ 

/******************************************************************************
*   Global Variable Definitions
******************************************************************************/

/******************************************************************************
*   Static Variable Definitions
******************************************************************************/
 static u8 SchM_OSTickEnabled;
 static u8 SchM_OSTickCounter;
 static u8 SchM_SchedulerEnabled;

/******************************************************************************
*   Global and Static Function Definitions
******************************************************************************/ 

/*****************************************************************************************************                                                                        
*   Function: SchM_Init(SchM_TaskConfigType *SchM_Config)
*
*   Description: Scheduler Initialization
*
*   Caveats:
*****************************************************************************************************/
void SchM_Init(void)
{
	/* Code of Example Here */
}


/*****************************************************************************************************                                                                        
*   Function: SchM_Init
*
*   Description: Scheduler De-initialization
*
*   Caveats:
*****************************************************************************************************/
void SchM_DeInit(void)
{
	/* Code of Example Here */
}


/*****************************************************************************************************                                                                        
*   Function: SchM_Start
*
*   Description: Starts the scheduler
*
*   Caveats:
*****************************************************************************************************/
void SchM_Start(void)
{
    SchM_SchedulerEnabled =  SCHEDULER_ENABLED;
}

/*****************************************************************************************************                                                                        
*   Function: SchM_Init
*
*   Description: Callback handled once an interrupt is generated by Channel 0.
*
*   Caveats: Non Reentrant
*****************************************************************************************************/
void SchM_OsTick(void)
{
  if(!SchM_OSTickEnabled)
  {
    SchM_OSTickEnabled = SCHM_OSTICK_ENABLED;
  }
  else
  {
    /* Moda faquin error */ 
  }
 
}

/*****************************************************************************************************                                                                        
*   Function: SchM_SchedulerCore
*
*   Description: BackGround Task
*
*   Caveats: Non Reentrant
*****************************************************************************************************/
void SchM_SchedulerCore(void)
{
   u8 Task_Index = 0;
   if(SchM_SchedulerEnabled)
   {
     if(SchM_OSTickEnabled)
     {
         SchM_OSTickCounter ++;
         while (Task_Index < SchM_TaskConfigInitial[0U].SchM_TaskNumberConfig)
         {
           if((SchM_OSTickCounter & SchM_TaskConfigInitial->ptr_SchM_Task[Task_Index].Mask) == 
               SchM_TaskConfigInitial->ptr_SchM_Task[Task_Index].Offset)
           {
             SchM_TaskConfigInitial->ptr_SchM_Task[Task_Index].SchM_TaskCallback();
           }
           Task_Index++;
         }
     }
     else
     {
        SchM_Background();
     }
   }
}
